package mo.leetcode_top;

import java.util.Arrays;

public class L66 {

    public int[] plusOne(int[] digits) {
        int carry = (digits[digits.length-1] + 1) / 10;
        digits[digits.length-1] = (digits[digits.length-1] + 1) % 10;
        for (int i = digits.length-2; i >= 0; i--) {
            if (carry == 0) {
                break;
            }
            int tmp = digits[i];
            digits[i] = (digits[i] + carry) % 10;
            carry = (tmp + carry) / 10;
        }
        if (carry == 0) {
            return digits;
        } else {
            int[] res = new int[digits.length+1];
            res[0] = 1;
            for (int i = 1; i < res.length; i++) {
                res[i] = digits[i-1];
            }
            return res;
        }
    }

    public static void main(String[] args) {
        L66 test = new L66();
        System.out.println(Arrays.toString(test.plusOne(new int[]{9, 9, 9, 9})));
    }

}
